<?php

class ActivityController extends BaseController
{

	/**
	 * Creates a new model.
	 * If creation is successful, the browser will be redirected to the 'view' page.
	 */
	public function actionCreate()
	{
		$model=new Activity;

		// Uncomment the following line if AJAX validation is needed
		// $this->performAjaxValidation($model);

		if(isset($_POST['Activity']))
		{
			$model->attributes=$_POST['Activity'];
			$model->zac_start_time = strtotime($model->zac_start_time.":00");
			$model->zac_end_time = strtotime($model->zac_end_time.":00");
			$model->zac_a_time = strtotime($model->zac_a_time.":00");
			$model->zac_e_time = strtotime($model->zac_e_time.":00");
			if($model->save())
				$this->redirect(array('admin'));
		}

		$this->render('create',array(
			'model'=>$model,
		));
	}

	/**
	 * Updates a particular model.
	 * If update is successful, the browser will be redirected to the 'view' page.
	 * @param integer $id the ID of the model to be updated
	 */
	public function actionUpdate($id)
	{
		$model=$this->loadModel($id);
		$model->zac_start_time = date('Y-m-d H',$model->zac_start_time);
		$model->zac_end_time = date('Y-m-d H',$model->zac_end_time);
		$model->zac_a_time = date('Y-m-d H',$model->zac_a_time);
		$model->zac_e_time = date('Y-m-d H',$model->zac_e_time);

		// Uncomment the following line if AJAX validation is needed
		// $this->performAjaxValidation($model);

		if(isset($_POST['Activity']))
		{
			$model->attributes=$_POST['Activity'];
			$model->zac_start_time = strtotime($model->zac_start_time.":00");
			$model->zac_end_time = strtotime($model->zac_end_time.":00");
			$model->zac_a_time = strtotime($model->zac_a_time.":00");
			$model->zac_e_time = strtotime($model->zac_e_time.":00");
			if($model->save())
				$this->redirect(array('admin'));
		}

		$this->render('update',array(
			'model'=>$model,
		));
	}

   /**
     *单条，批量删除
     *传入参数，$GET单条，$POST批量
     */
    public function actionDelete(){
        //操作ID号格式处理
        if(isset($_GET['id'])){
            $id = $_GET['id'];
        }elseif(isset($_POST['ids'])){
            $id =$_POST['ids'];
        }else{
            throw new CHttpException(400,'Invalid request. Please do not repeat this request again.');
        }

        //处理条件
        $criteria=new CDbCriteria;
        $criteria->addInCondition("zac_id",(array)$id);

        //执行操作
        if(Activity::model()->deleteAll($criteria)){
            $this->redirect(isset($_POST['returnUrl']) ? $_POST['returnUrl'] : array('admin'));
        }
    }
	
	/**
	 * Manages all models.
	 */
	public function actionAdmin()
	{
		$model=new Activity('search');
		$model->unsetAttributes();  // clear any default values
		if(isset($_GET['Activity']))
			$model->attributes=$_GET['Activity'];

		$this->render('admin',array(
			'model'=>$model,
		));
	}

	/**
	 * Returns the data model based on the primary key given in the GET variable.
	 * If the data model is not found, an HTTP exception will be raised.
	 * @param integer the ID of the model to be loaded
	 */
	public function loadModel($id)
	{
		$model=Activity::model()->findByPk($id);
		if($model===null)
			throw new CHttpException(404,'The requested page does not exist.');
		return $model;
	}

	/**
	 * Performs the AJAX validation.
	 * @param CModel the model to be validated
	 */
	protected function performAjaxValidation($model)
	{
		if(isset($_POST['ajax']) && $_POST['ajax']==='activity-form')
		{
			echo CActiveForm::validate($model);
			Yii::app()->end();
		}
	}
}
